#!/usr/bin/env python
# coding=utf-8

from flask_script import Manager, Server
from livereload import Server, shell

from over.app import app

server = Server(app.wsgi_app)

manager = Manager(app)
# manager.add_command('runserver', Server(host='0.0.0.0', port=5000))

@manager.command
def createdb():
    from over.model import db
    db.create_all()


@manager.command
def dropdb():
    from over.model import db

    db.drop_all()


@manager.command
def addserver():
    from over.model import db, Server, ServerPrivateKey
    server = Server(name=u'日本机房', ip="45.63.123.226", location=u'日本', desc=u'测试')
    db.session.add(server)

    with open('/Users/feipeng/.ssh/id_rsa_person') as fp:
        private_key = fp.read()

    server_account = ServerPrivateKey(server_id=server.id, username='root', private_key=private_key)
    db.session.add(server_account)

    db.session.commit()


@manager.command
def initdb():
    from over.model import db, User, Product

    db.create_all()

    user = User(email='netubu@gmail.com', password='123456', status=1, balance=10000, role='admin')
    db.session.add(user)

    description = u'''2个美国节点,分别位于Fremont和Los Angeles，高带宽

    7 个亚洲节点，分别位于新加坡、日本和香港，低延迟

    针对普通上网行为不强行限制流量，**但流量不允许滥用（如长时间观看在线视频或下载大文件等，严禁BT等P2P行为），滥用流量将被封停服务并不退款**

    同一时间同一个账号仅限一个终端使用，如需多个账号请联系客户获取优惠。

    未越狱 iOS 用户需要另外购买第三方客户端。
    '''
    product = Product(title=u'普通版', description=description, price=6800, period=Product.PERIOD_YEAR)
    db.session.add(product)

    product = Product(title=u'普通版', description=description, price=800, period=Product.PERIOD_MONTH)
    db.session.add(product)

    db.session.commit()

    # addserver()


def main():
    # server.watch
    server.serve()
    # manager.run()


if __name__ == '__main__':
    main()
